import { Component, OnInit, Output, Input, EventEmitter } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import { _HttpClient } from '@delon/theme';
import { HttpParams } from '@angular/common/http';
import { environment } from '../../../../environments/environment';

@Component({
    selector: 'app-addyear',
    templateUrl: './addyear.component.html',
    styles: [`
        .red{color:#ff0000}
    `]
})
export class AddYearComponent implements OnInit {
    Visible = false;
    yearValue: any;
    workValue = '';
    workList = [];
    dataValue = '0';
    dataList = [{ 'value': '0', 'label': '不生成' }, { 'value': '1', 'label': '生成' }];

    @Input() operateType: any;
    @Output() public yearCloseModal = new EventEmitter();
    @Output() public yearReloadData = new EventEmitter();

    constructor(
        private message: NzMessageService,
        private http: _HttpClient
    ) { }

    ngOnInit() {
        const date = new Date;
        this.yearValue = date.getFullYear();

        setTimeout(() => {
            this.Visible = true;
            // dictClass值写死
            this.http.get(environment.apiBase + '/csmis/sysdict/getSysdictsByClass.jwt?dictClass=YEAR_WORK_TYPE').subscribe((res: any) => {
                if (res.type === "success") {
                    if (res.data.length) {
                        this.workList = res.data;
                    }
                }
            });
        }, 200);
    }

    Ok() {
        if (this.workValue === '') {
            this.message.create('error', '请选择年表作业类型');
            return;
        }
        const params = new HttpParams()
            .set('yearnum', this.yearValue)
            .set('linetype', this.workValue)
            .set('generate', this.dataValue)
            .set('vcOrgId', '');
        this.http.post(environment.apiBase + '/csmis/ymyear/ymtyearwork/getInsertYearplan.jwt', params).subscribe(res => {
            if (res.type === 'success') {
                this.message.create('success', '生成成功');
                this.yearReloadData.emit();
            } else {
                if (res.message === '') {
                    this.message.create('success', '生成错误');
                } else {
                    this.message.create('error', res.message);
                }
            }
            this.Visible = false;
            setTimeout(() => {
                this.yearCloseModal.emit();
            }, 200);
        });
    }

    Cancel() {
        this.Visible = false;
        setTimeout(() => {
            this.yearCloseModal.emit();
        }, 200);
    }
}